home *** CD-ROM | disk | FTP | other *** search
- Path: news.mindlink.net!news
- From: genew@mindlink.bc.ca (Gene Wirchenko)
- Newsgroups: comp.lang.c
- Subject: Re: sscanf problems
- Date: Wed, 24 Jan 1996 05:43:38 GMT
- Organization: MIND LINK! - British Columbia, Canada
- Message-ID: <4e4h0j$af7@fountain.mindlink.net>
- References: <4e4c2v$j2g@mathserv.mps.ohio-state.edu>
- NNTP-Posting-Host: line257.nwm.mindlink.net
- X-Newsreader: Forte Free Agent 1.0.82
-
- Chris Mongold <cmongold@magnus.acs.ohio-state.edu> wrote:
-
- >Hello,
- > I'm sorry if this is an inappropriate topic, but I've tried
- >everything else. I can't seem to get sscanf to to separate a string
- >into various variable types. Here is an example:
-
- >#include <stdio.h>
-
- >void main()
- ^
- This is a no-no. Per the Standard, main() returns int.
-
- >{
- >char input[20], crap[17], segment[6], seg_len[3], begin[3], load[3];
- >int type;
- >FILE *fp;
-
- >printf("File: ");
- >gets(input);
-
- Buffer can be overflowed. You might want to use fgets().
-
- >fp = fopen(input, "r");
-
- You don't check if the file could be opened. This may be the
- problem. Add:
-
- if (fp==NULL)
- {
- printf("Input file could not be opened.\n");
- exit(EXIT_FAILURE);
- }
-
- >while(!feof(fp))
- >{
- >fgets(crap, 17, fp);
- >sscanf(crap, "%d%3s%6s%3s%3s", type, begin, segment, seg_len, load);
- >printf("%d %3s %6s %3s %3s", type, begin, segment, seg_len, load);
- ^
- type is never assigned a value.
-
- >}
-
- Since main() returns a value, return one. Add:
- return 0;
-
- >}
- >No matter what I do, it always 'bus errors' when I sscanf. I've tried
- >it several different ways, including making crap larger, but to
- >no avail. If anyone can help me, I would greatly appreciate it.
-
- I wonder if your data is in the wrong format. Include a copy of
- it.
- I suspect you'll need to copy the chars to their dest.
-
- >Please respond to cmongold@magnus.acs.ohio-state.edu
-
- >Thanks,
-
- >Chris
-
- Sincerely,
-
- Gene Wirchenko
-
- C Pronunciation Guide:
- y=x++; "wye equals ex plus plus semicolon"
- x=x++; "ex equals ex doublecross semicolon"
-
-